from cerebra import *
import scipy
import scipy.interpolate 
import string, glob
from cmath import *
class node(cerebra.network):
	def __init__(self,ID, (parents={}, children={}, offset, neighbors={}), (estring, cstrings, sstrings), (resolution, spectrum, number, cell_width, frame_width, mass)):
		network.__init__(network, resolution, spectrum, number, cell_width, frame_width, mass)
		self.ID=ID
		self.parents=parents
		self.children=children
		self.neighbors
		self.offset=offset
		self.estring=string
		self.cstrings=cstrings
		self.sstrings=sstrings
	def sub_evolve(self):
		for k in children.keys():
			i=children[i]
			i.sub_evolve()
			for a in xrange(self.spectrum):
				if(i.sstrings[a] in self.sstrings):
					x = scipy.linspace(i.offset[0],i.offset[0]+i.res_x*i.cellwidth, i.cellwidth)
					y = scipy.linspace(i.offset[0],i.offset[0]+i.res_y*i.cellwidth, i.cellwidth)
					z = i.number[a]
					spl = scipy.interpolate.RectBivariateSpline(x,y,z) 
					nx=scipy.linspace(i.offset[0],i.offset[0]+i.res_x*i.cellwidth, self.cellwidth)
					ny=scipy.linspace(i.offset[0],i.offset[0]+i.res_y*i.cellwidth, self.cellwidth)
					self.number[a]+=spl(nx,ny)
					self.network+=a*self.number[a]*self.mass*exp((0+1.0j)
					self.lambda1[a]+=i.lambda1[a]*(i.res_x*i.res_y*i.cellwidth**2)/(self.res_x*self.res_y*self.cellwidth**2)
					self.lambda2[a]+=i.lambda2[a]*(i.res_x*i.res_y*i.cellwidth**2)/(self.res_x*self.res_y*self.cellwidth**2)
					counter++
			
	def append_child(self,child, string):
		self.children[int(abs(child.ID-self.ID))]=child
		self.cstrings[int(abs(child.ID-self.ID))]=string
		child.parents[int(abs(child.ID-self.ID))]=parent
	def attach_to_parent(self, parent):
		parent.append_child(self, self.estring)
	def change_string(self,string_new):
		self.cstring=string_new
		for i in self.parents
			i.cstrings[int(abs(i.ID-self.ID))]=string_new
	def save(f1, f2, f3, f4):
		open(f1+".%i"%(self.ID)).append("%s %s %s %s %s %i \n"%(self.estring, self.cstrings.values().join('|'), self.sstrings.().join('|'), self.sstrings.values().join('|'), f2, f3, string.join([str(self.children[i].ID) for i in self.children.keys()],"|"), self.ID, f4))
		self.number.tofile(f2)
		self.spectrum.tofile(f3)
		open(f4).write("%i %i %f %f %f"%(self.res_x, self.res_y, self.cellwidth, self.timestep, self.mass)
	def load(f1):
		fstring=open(f1).read()
		self.ID=int(fstring.split(' ')[7])
		self.estring=int(fstring.split(' ')[0])
		f2=self.ID=int(fstring.split(' ')[4])
		f3=int(fstring.split(' ')[5])
		f4=int(fstring.split(' ')[8])
		for i in xrange(len(fstring.split(' ')[2].split('|')))
			self.sstring[int(fstring.split(' ')[2].split('|')[i])]=fstring.split(' ')[3].split('|')[i]
		for i in xrange(len(fstring.split(' ')[6].split('|'))):
			self.cstring[abs(int(fstring.split(' ')[6].split('|')[i])-self.ID)]=fstring.split(' ')[1].split('|')[i]
		for i in glob.glob('%s.*'%string.join(f1.split('.')[0:len(f1.split('.'))-1],'.')):
			if (abs(int(open(i).read().split(' ')[7])-self.ID) in self.cstring.keys) and (open(i).read().split(' ')[0] in self.cstrings):
				children[(abs(int(open(i).read().split(' ')[7])-self.ID)]=load(i)
		fstring=open(f4).read().split(' ')
		self.res_x = fstring[0]
		self.res_y = fstring[1]
		self.cellwidth=fstring[2]
		self.timestep=fstring[3]
		self.mass=fstring[4]
		self.number=array.fromfile(f2)
		self.spectrum=array.fromfile(f3)
		return self


